<?php

namespace app\common\model\xilujob;

use think\Db;
use think\Exception;
use think\Model;

class JobCall extends Model
{
    protected $name = 'xilujob_job_call';
    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = false;

    public static function getJobCall($userId, $jobId) {
        if(!$userId || !$jobId) {
            throw new Exception('参数错误');
        }
        $applied = JobApply::where('user_id',$userId)->where('job_id', $jobId)->count()>0;
        if(!$applied) {
            throw new Exception('请先投递简历，才能查看联系方式');
        }

        $jobCall = self::where('user_id', $userId)
            ->where('job_id', $jobId)
            ->find();
        if($jobCall) {
            return $jobCall;
        }
        $job = Job::field('id,publisher_id')->find($jobId);
        if(!$job) {
            throw new Exception('invalid arguments');
        }
        $jobCall = JobCall::create(['user_id'=>$userId,'job_id'=>$job->id, 'company_id'=>$job->publisher_id]);

        //更新企业拨打电话数
        CompanyStatistics::where('id', $job->publisher_id)->update(['job_call_count'=>Db::raw('job_call_count+1')]);

        return $jobCall;
    }

}
